package com.iwunu.gateway.controller;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.iwunu.gateway.cache.RedisService;
import com.iwunu.gateway.config.UrlConfig;
import com.iwunu.gateway.constant.ApartmentConstant;
import com.iwunu.gateway.entity.FLockMember;
import com.iwunu.gateway.entity.FaceLock;
import com.iwunu.gateway.entity.ImageMongo;
import com.iwunu.gateway.service.CmdIssuedMongodbService;
import com.iwunu.gateway.service.FaceLockMemberService;
import com.iwunu.gateway.service.FaceLockService;
import com.iwunu.gateway.service.FacesLogMongodbService;
import com.iwunu.gateway.service.FlockLogService;
import com.iwunu.gateway.service.ImageFeatureService;
import com.iwunu.gateway.service.ImageMongodbService;
import com.iwunu.gateway.service.TokenService;
import com.iwunu.gateway.util.ComUtil;
import com.iwunu.gateway.util.FileUtils;
import com.iwunu.gateway.util.PitNumUtil;
import com.iwunu.gateway.vo.CmdIssuedVo;
import com.iwunu.gateway.vo.LogVo;
import com.iwunu.gateway.vo.UserCmdIssuedVo;
import com.iwunu.iot.common.constant.ClientOpenTypeEnum;
import com.iwunu.iot.common.constant.Constant;
import com.iwunu.iot.common.constant.ResultCodeEnum;
import com.iwunu.iot.common.util.HexUtils;
import com.iwunu.iot.common.util.Result;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiParam;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

@RestController
@Api(value = "lock/api", tags = "人脸锁控制层接口", description = "人脸锁控制层接口")
public class OrgFacesLockApiController {
    @Autowired
    public RedisService  redisService;
    @Autowired
    public FaceLockService faceLockService;
    @Autowired
    public FlockLogService flockLogService;
    @Autowired
    private ImageMongodbService imageMongodbService;
    @Autowired
    private CmdIssuedMongodbService cmdIssuedMongodbService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private FaceLockMemberService faceLockMemberService;
    @Autowired
    private ImageFeatureService imageFeatureService;
    @Autowired
    private  UrlConfig urlConfig;
    
    @Autowired
    private  FacesLogMongodbService facesLogMongodbService;
    private static final String fileUploadPath = "/opt/img";
    /**
     **下发密码
     * @return
     */
    @SuppressWarnings({ "unchecked", "rawtypes" })
    @ApiImplicitParams({
        @ApiImplicitParam(name = "cmdIssued", value = "{\"cmdData\":[{\"data\":\"258369\""
        		+ ",\"startTime\":\"2021-06-10 00:00:00\",</br>\"endTime\":\"2021-06-10 00:00:00\"}]}"
                , required = true, dataType = "String",paramType="body") })
	@RequestMapping(value = "/org/password/{lockId}" , method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Result> cmdIssued(
    		 @RequestHeader String Authorization,
    		 @ApiParam(value = "锁Id", required = true) @PathVariable("lockId") Integer lockId,
    		 @RequestBody JSONObject cmdIssuedVos
            ){
    	 Result result = null ;
      	 String userId = tokenService.parseOrgToken(Authorization);
      	 FLockMember flock = faceLockMemberService.findFaceLockMember(Integer.valueOf(userId), lockId);
         if(StringUtils.isEmpty(Authorization)|| flock ==null||cmdIssuedVos.size()<=0) {
           	 result = new Result(ResultCodeEnum.INVALID_PARAMS.getCode(), ResultCodeEnum.INVALID_PARAMS.getDesc(),null);
            	 return new ResponseEntity<>(result, HttpStatus.OK);
         }
         String cmds = JSONArray.toJSONString(cmdIssuedVos.get("cmdData"));
         List<CmdIssuedVo> cmdList = JSONArray.parseArray(cmds, CmdIssuedVo.class);
         String code = cmdIssuedMongodbService.saveSendImageData(cmdList, userId, flock.getLockClientId(),  ClientOpenTypeEnum.PASSWORD.getType(),flock.getRole());
         if (!code.isEmpty()) {
           	 result = new Result(ResultCodeEnum.OVER_PITNUM.getCode(), ResultCodeEnum.OVER_PITNUM.getDesc(),null);
           	 return new ResponseEntity<>(result, HttpStatus.OK);
         }
       	 result = new Result(ResultCodeEnum.CMDISSUED_SUCCESS.getCode(), ResultCodeEnum.CMDISSUED_SUCCESS.getDesc(),null);
       	 return new ResponseEntity<>(result, HttpStatus.OK);
    }
    /**
     **下发卡号
     * @return
     */
    @SuppressWarnings({ "unchecked", "rawtypes" })
    @ApiImplicitParams({
        @ApiImplicitParam(name = "cardNo", value = "{\"cmdData\":[{\"data\":\"C90178CF\""
        		+ ",\"startTime\":\"2021-06-10 00:00:00\",</br>\"endTime\":\"2021-06-10 00:00:00\"}]}"
                , required = true, dataType = "String",paramType="body") })
	@RequestMapping(value = "/org/cardNo/{lockId}" , method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Result> cardNo(
    		 @RequestHeader String Authorization,
    		 @ApiParam(value = "锁Id", required = true) @PathVariable("lockId") Integer lockId,
    		 @RequestBody JSONObject cmdIssuedVos
            ){
    	 Result result = null ;
      	 String userId = tokenService.parseOrgToken(Authorization);
      	 FLockMember flock = faceLockMemberService.findFaceLockMember(Integer.valueOf(userId), lockId);
         if(StringUtils.isEmpty(Authorization)|| flock ==null||cmdIssuedVos.size()<=0) {
           	 result = new Result(ResultCodeEnum.INVALID_PARAMS.getCode(), ResultCodeEnum.INVALID_PARAMS.getDesc(),null);
            	 return new ResponseEntity<>(result, HttpStatus.OK);
         }
         String cmds = JSONArray.toJSONString(cmdIssuedVos.get("cmdData"));
         List<CmdIssuedVo> cmdList = JSONArray.parseArray(cmds, CmdIssuedVo.class);
         String code = cmdIssuedMongodbService.saveSendImageData(cmdList, userId, flock.getLockClientId(), ClientOpenTypeEnum.CARD.getType(),flock.getRole());
         if (!code.isEmpty()) {
           	 result = new Result(ResultCodeEnum.CMDISSUED_SUCCESS.getCode(), ResultCodeEnum.CMDISSUED_SUCCESS.getDesc(),null);
           	 return new ResponseEntity<>(result, HttpStatus.OK);
         }
       	 result = new Result(ResultCodeEnum.CMDISSUED_SUCCESS.getCode(), ResultCodeEnum.CMDISSUED_SUCCESS.getDesc(),null);
       	 return new ResponseEntity<>(result, HttpStatus.OK);
    }
    /**
     **下发指纹
     * @return
     */
    @SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping(value = "/org/finger/{lockId}" , method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Result> finger(
    		 @RequestHeader String Authorization,
    		 @ApiParam(value = "锁Id", required = true) @PathVariable("lockId") Integer lockId,
    		 @RequestBody JSONObject cmdIssuedVos
            ){
    	 Result result = null ;
      	 String userId = tokenService.parseOrgToken(Authorization);
      	 FLockMember flock = faceLockMemberService.findFaceLockMember(Integer.valueOf(userId), lockId);
      	  String cmds = JSONArray.toJSONString(cmdIssuedVos.get("cmdData"));
          List<CmdIssuedVo> cmdList = JSONArray.parseArray(cmds, CmdIssuedVo.class);
         if(StringUtils.isEmpty(Authorization)|| flock ==null||cmdIssuedVos.size()<=0) {
           	 result = new Result(ResultCodeEnum.INVALID_PARAMS.getCode(), ResultCodeEnum.INVALID_PARAMS.getDesc(),null);
            	 return new ResponseEntity<>(result, HttpStatus.OK);
         }
         String code = cmdIssuedMongodbService.saveSendImageData(cmdList, userId, flock.getLockClientId(), ClientOpenTypeEnum.CARD.getType(),flock.getRole());
         if (!code.isEmpty()) {
           	 result = new Result(ResultCodeEnum.CMDISSUED_SUCCESS.getCode(), ResultCodeEnum.CMDISSUED_SUCCESS.getDesc(),null);
           	 return new ResponseEntity<>(result, HttpStatus.OK);
         }
       	 result = new Result(ResultCodeEnum.CMDISSUED_SUCCESS.getCode(), ResultCodeEnum.CMDISSUED_SUCCESS.getDesc(),null);
       	 return new ResponseEntity<>(result, HttpStatus.OK);
    }
    /**
     **人脸图像下发
     * @return
     */
    @SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping(value = "/org/facesImage/{lockId}" , method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public  ResponseEntity<Result>  uploadThumbnail(
    	@RequestHeader String Authorization,	
    	@RequestParam("facesImage")MultipartFile[] multipartFiles,
    	 @ApiParam(value = "锁Id", required = true) @PathVariable("lockId") Integer lockId,
    	@ApiParam(value = "有效开始时间", required = true) @RequestParam("startTime") String startTime,
    	@ApiParam(value = "有效结束时间", required = true) @RequestParam("endTime") String endTime,
    	@ApiParam(value = "备注", required = true) @RequestParam("remark") String remark
    	) throws Exception {
    	
    	 Result result = null ;
        Integer type =  ClientOpenTypeEnum.FACES.getType();
     	String userId = tokenService.parseOrgToken(Authorization);
     	FLockMember flock = faceLockMemberService.findFaceLockMember(Integer.valueOf(userId), lockId);
        if(StringUtils.isEmpty(Authorization)|| flock ==null) {
          	 result = new Result(ResultCodeEnum.INVALID_PARAMS.getCode(), ResultCodeEnum.INVALID_PARAMS.getDesc(),null);
        	 return new ResponseEntity<>(result, HttpStatus.OK);
        }
        List<ImageMongo> images = imageMongodbService.getSendImageData(flock.getLockClientId());
        if(images.size()>ApartmentConstant.imagePitNum) {
          	 result = new Result(ResultCodeEnum.INVALID_PARAMS.getCode(), ResultCodeEnum.INVALID_PARAMS.getDesc(),null);
        	 return new ResponseEntity<>(result, HttpStatus.OK);
        }
         
        if(!ComUtil.isEmpty(multipartFiles) && multipartFiles.length != 0) {
            for (MultipartFile multipartFile : multipartFiles) {
            	Integer pitNum = PitNumUtil.getImagePitNum(images);
                String postFix = multipartFile.getOriginalFilename().split("//.")[multipartFile.getOriginalFilename().split("//.").length-1];
                if(Arrays.asList(Constant.FilePostFix.IMAGES).contains(postFix)){
                 	 result = new Result(ResultCodeEnum.INVALID_PARAMS.getCode(), ResultCodeEnum.INVALID_PARAMS.getDesc(),null);
                	 return new ResponseEntity<>(result, HttpStatus.OK);
                }
                String url =FileUtils.fileSave(multipartFile.getInputStream(), 1, multipartFile.getOriginalFilename(), null);
                //压缩图片
               String preUrl = FileUtils.thumbnails(url, url);
               if(StringUtils.isNoneEmpty(preUrl)) {
					byte[] imageDataFeature = imageFeatureService.getImageFeature(multipartFile.getBytes());
					if (imageDataFeature == null) {
						result = new Result(ResultCodeEnum.CMDISSUED_FAIL.getCode(),
								ResultCodeEnum.CMDISSUED_FAIL.getDesc(), null);
						return new ResponseEntity<>(result, HttpStatus.CREATED);
					}
            	   byte[] imageData = FileUtils.getImageFromLocalByUrl(url);
					String data = HexUtils.bytesToHexString(imageDataFeature, imageDataFeature.length);
            	   preUrl = urlConfig.getImageHost()+preUrl.replace(fileUploadPath, "");
            	   imageMongodbService.saveSendImageData(startTime, endTime, imageData,imageDataFeature, userId, pitNum, preUrl,flock.getLockClientId(),flock.getRole(),remark);
            	   JSONObject json = new JSONObject();
            	   json.put("flDeviceId", Authorization);
            	   json.put("clientId", flock.getLockClientId());
            	   json.put("pitNum", pitNum);
            	   json.put("size", imageData.length);
            	   json.put("startTime", startTime);
            	   json.put("feature", data);
            	   json.put("endTime", endTime);
            	   redisService.rightPush(flock.getLockClientId()+"##_"+type, json.toJSONString());
            		redisService.rightPush(flock.getLockClientId() + "##_" + ClientOpenTypeEnum.FEATURE.getType(), json.toJSONString());
//            	  FileUtils.deleteFile(url);
               }
            }
        }
      	 result = new Result(ResultCodeEnum.CMDISSUED_SUCCESS.getCode(), ResultCodeEnum.CMDISSUED_SUCCESS.getDesc(),null);
      	 return new ResponseEntity<>(result, HttpStatus.OK);
    }
    /**
     **开锁
     * @return
     */
    @RequestMapping(value = "/org/openLock/{lockId}/{status}" , method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Result>  openLock(
	   		 @RequestHeader String Authorization,
	   		 @ApiParam(value = "锁Id", required = true) @PathVariable("lockId") Long lockId,
	     	 @ApiParam(value = "开门转态1:开门后常开0:开门", required = true) @PathVariable("status") Integer status
    		){
      	 Result result = null ;
      	 String type =  ClientOpenTypeEnum.OPEN.getData();
      	 String userId = tokenService.parseOrgToken(Authorization);
      	 FLockMember flock = faceLockMemberService.findFaceLockMember(Integer.valueOf(userId), lockId.intValue());
        if(StringUtils.isEmpty(Authorization)|| flock ==null) {
          	 result = new Result(ResultCodeEnum.INVALID_PARAMS.getCode(), ResultCodeEnum.INVALID_PARAMS.getDesc(),null);
           	 return new ResponseEntity<>(result, HttpStatus.OK);
        }
        String key = flock.getLockClientId()+type;
        if(  redisService.get(key)==null) {
        	redisService.setex(key, 120, status.toString());
//        	System.out.println("key:"+key + "----redisService.get(key):"+ redisService.get(key)+":"+type);
        	flockLogService.saveLog(userId, status.toString(), flock.getLockClientId(), lockId,ClientOpenTypeEnum.OPEN.getType(),flock.getRole());
        }
       	 result = new Result(ResultCodeEnum.OK.getCode(), ResultCodeEnum.OK.getDesc(),null);
       	 return new ResponseEntity<>(result, HttpStatus.OK);
    }
    /**
     **删除密码
     * @return
     */
    @RequestMapping(value = "/org/delPwd/{lockId}/{pitNum}" , method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Result>  delPwd(
	   		 @RequestHeader String Authorization,
	   		 @ApiParam(value = "锁Id", required = true) @PathVariable("lockId") Integer lockId,
	     	 @ApiParam(value = "坑号", required = true) @PathVariable("pitNum") Integer pitNum
    		){
      	 Result result = null ;
      	 String type =  ClientOpenTypeEnum.DELPWD.getData();
      	 String userId = tokenService.parseOrgToken(Authorization);
      	 FLockMember flock = faceLockMemberService.findFaceLockMember(Integer.valueOf(userId), lockId);
        if(StringUtils.isEmpty(Authorization)|| flock ==null) {
          	 result = new Result(ResultCodeEnum.INVALID_PARAMS.getCode(), ResultCodeEnum.INVALID_PARAMS.getDesc(),null);
           	 return new ResponseEntity<>(result, HttpStatus.OK);
        }
         String key = flock.getLockClientId()+type;
         redisService.rightPush(key, pitNum.toString());
         cmdIssuedMongodbService.updateCmd(flock.getLockClientId(), pitNum,ClientOpenTypeEnum.DELPWD.getType());
       	 result = new Result(ResultCodeEnum.OK.getCode(), ResultCodeEnum.OK.getDesc(),null);
       	 return new ResponseEntity<>(result, HttpStatus.OK);
    }
    /**
     **删除卡
     * @return
     */
    @RequestMapping(value = "/org/delCard/{lockId}/{pitNum}" , method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Result>  delCard(
	   		 @RequestHeader String Authorization,
	   		 @ApiParam(value = "锁Id", required = true) @PathVariable("lockId") Integer lockId,
	     	 @ApiParam(value = "坑号", required = true) @PathVariable("pitNum") Integer pitNum
    		){
      	 Result result = null ;
      	 String type =  ClientOpenTypeEnum.DELCARD.getData();
      	String userId = tokenService.parseOrgToken(Authorization);
      	 FLockMember flock = faceLockMemberService.findFaceLockMember(Integer.valueOf(userId), lockId);
        if(StringUtils.isEmpty(Authorization)|| flock ==null) {
          	 result = new Result(ResultCodeEnum.INVALID_PARAMS.getCode(), ResultCodeEnum.INVALID_PARAMS.getDesc(),null);
           	 return new ResponseEntity<>(result, HttpStatus.OK);
        }
         String key = flock.getLockClientId()+type;
         redisService.rightPush(key, pitNum.toString());
         cmdIssuedMongodbService.updateCmd(flock.getLockClientId(), pitNum,ClientOpenTypeEnum.DELPWD.getType());
       	 result = new Result(ResultCodeEnum.OK.getCode(), ResultCodeEnum.OK.getDesc(),null);
       	 return new ResponseEntity<>(result, HttpStatus.OK);
    }   
    /**
     **删除指纹
     * @return
     */
    @RequestMapping(value = "/org/delFinger/{lockId}/{pitNum}" , method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Result>  delFinger(
	   		 @RequestHeader String Authorization,
	   		 @ApiParam(value = "锁Id", required = true) @PathVariable("lockId") Integer lockId,
	     	 @ApiParam(value = "坑号", required = true) @PathVariable("pitNum") Integer pitNum
    		){
      	 Result result = null ;
      	 String type =  ClientOpenTypeEnum.DELFINGER.getData();
      	 String userId = tokenService.parseOrgToken(Authorization);
      	 FLockMember flock = faceLockMemberService.findFaceLockMember(Integer.valueOf(userId), lockId);
        if(StringUtils.isEmpty(Authorization)|| flock ==null) {
          	 result = new Result(ResultCodeEnum.INVALID_PARAMS.getCode(), ResultCodeEnum.INVALID_PARAMS.getDesc(),null);
           	 return new ResponseEntity<>(result, HttpStatus.OK);
        }
        String key = flock.getLockClientId()+type;
        redisService.rightPush(key, pitNum.toString());
        cmdIssuedMongodbService.updateCmd(flock.getLockClientId(), pitNum,ClientOpenTypeEnum.DELPWD.getType());
       	 result = new Result(ResultCodeEnum.OK.getCode(), ResultCodeEnum.OK.getDesc(),null);
       	 return new ResponseEntity<>(result, HttpStatus.OK);
    }    
    /**
     **删除人脸
     * @return
     */
    @RequestMapping(value = "/org/delFaces/{lockId}/{pitNum}" , method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Result>  delFaces(
	   		 @RequestHeader String Authorization,
	   		 @ApiParam(value = "锁Id", required = true) @PathVariable("lockId") Integer lockId,
	     	 @ApiParam(value = "坑号", required = true) @PathVariable("pitNum") Integer pitNum
    		){
      	 Result result = null ;
      	 String type =  ClientOpenTypeEnum.DELFACES.getData();
      	 String userId = tokenService.parseOrgToken(Authorization);
      	 FLockMember flock = faceLockMemberService.findFaceLockMember(Integer.valueOf(userId), lockId);
        if(StringUtils.isEmpty(Authorization)|| flock ==null) {
          	 result = new Result(ResultCodeEnum.INVALID_PARAMS.getCode(), ResultCodeEnum.INVALID_PARAMS.getDesc(),null);
           	 return new ResponseEntity<>(result, HttpStatus.OK);
        }
        String key = flock.getLockClientId()+type;
        redisService.rightPush(key, pitNum.toString());
       	result = new Result(ResultCodeEnum.OK.getCode(), ResultCodeEnum.OK.getDesc(),null);
       	return new ResponseEntity<>(result, HttpStatus.OK);
    }
    /**
     **获取用户人脸
     * @return
     */
    @RequestMapping(value = "/org/facesList/{lockId}" , method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Result>  facesList(
	   		 @RequestHeader String Authorization,
	   		 @ApiParam(value = "锁Id", required = true) @PathVariable("lockId") Integer lockId
    		){
      	 Result result = null ;
      	String type = ClientOpenTypeEnum.FACES.getData();
      	 String userId = tokenService.parseOrgToken(Authorization);
      	 Map<String, Object> fl = faceLockMemberService.findLockInfo(Integer.valueOf(userId), lockId);
        if(fl ==null || fl.isEmpty()) {
        	 result = new Result(ResultCodeEnum.NOT_GATEWAY.getCode(), ResultCodeEnum.NOT_GATEWAY.getDesc(),null);
           	 return new ResponseEntity<>(result, HttpStatus.OK);
        }
        List<UserCmdIssuedVo> userCmdIssueds = faceLockService.getUserImageData(fl);
       	 result = new Result(ResultCodeEnum.OK.getCode(), ResultCodeEnum.OK.getDesc(),userCmdIssueds);
       	 return new ResponseEntity<>(result, HttpStatus.OK);
    }
    
    /**
     **获取用户人脸
     * @return
     */
    @RequestMapping(value = "/org/passwordList/{lockId}" , method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Result>  passwordList(
	   		 @RequestHeader String Authorization,
	   		 @ApiParam(value = "锁Id", required = true) @PathVariable("lockId") Integer lockId
    		){
      	 Result result = null ;
      	Integer type = ClientOpenTypeEnum.PASSWORD.getType();
      	 String userId = tokenService.parseOrgToken(Authorization);
      	 Map<String, Object> fl = faceLockMemberService.findLockInfo(Integer.valueOf(userId), lockId);
        if(fl ==null) {
          	 result = new Result(ResultCodeEnum.NOT_GATEWAY.getCode(), ResultCodeEnum.NOT_GATEWAY.getDesc(),null);
           	 return new ResponseEntity<>(result, HttpStatus.OK);
        }
         List<UserCmdIssuedVo> userCmdIssueds = faceLockService. getUserCmdIssude(fl, type);
       	 result = new Result(ResultCodeEnum.OK.getCode(), ResultCodeEnum.OK.getDesc(),userCmdIssueds);
       	 return new ResponseEntity<>(result, HttpStatus.OK);
    }
    
    /**
     **获取用户人脸
     * @return
     */
    @RequestMapping(value = "/org/loglist/{lockId}" , method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Result>  loglist(
	   		 @RequestHeader String Authorization,
	         @ApiParam(value = "每页显示数量", required = true) @RequestParam("pageSize") Integer pageSize,
	   		 @ApiParam(value = "当前页", required = true) @RequestParam("pageNum") Integer currentPage,
	   		 @RequestParam(value = "startTime", required = false)String startTime,
	   		 @RequestParam(value = "endTime ", required = false) String endTime,
	   		 @RequestParam(value = "openType", required = false) Integer openType,
	   		 @RequestParam(value = "isSucc", required = false) boolean isSucc,
	   		 @RequestParam(value = "isOwner", required = true) boolean isOwner,
	   		 @ApiParam(value = "锁Id", required = true) @PathVariable("lockId") Integer lockId
    		){
      	Result result = null ;
      	LogVo log = new LogVo();
      	log.setCurrentPage(currentPage);
      	log.setStartTime(startTime);
      	log.setEndTime(endTime);
      	log.setOwner(isOwner);
      	log.setPageSize(pageSize);
      	log.setSucc(isSucc);
      	log.setType(openType);
      	Map<String, Object> logs = null;
      	 String userId = tokenService.parseOrgToken(Authorization);
      	 Map<String, Object> fl = faceLockMemberService.findLockInfo(Integer.valueOf(userId), lockId);
        if(fl ==null) {
          	 result = new Result(ResultCodeEnum.NOT_GATEWAY.getCode(), ResultCodeEnum.NOT_GATEWAY.getDesc(),null);
           	 return new ResponseEntity<>(result, HttpStatus.OK);
        }
        if(!log.isOwner()) {
        	logs = facesLogMongodbService.getOtherLog(lockId,userId, fl.get("lockclientid").toString(), log, Integer.valueOf(fl.get("role").toString()));
        }else {
        	logs = facesLogMongodbService.getMyLog(fl, log);
        }
       	 result = new Result(ResultCodeEnum.OK.getCode(), ResultCodeEnum.OK.getDesc(),logs);
       	 return new ResponseEntity<>(result, HttpStatus.OK);
    }
    
    /**
     **更新配网状态
     * @return
     */
    @RequestMapping(value = "/org/updateNetWorK/{lockId}" , method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Result>  updateNetWorK(
	   		 @RequestHeader String Authorization,
	   		 @ApiParam(value = "锁Id", required = true) @PathVariable("lockId") Integer lockId,
	   		 @RequestParam(value = "netWorkStatus", required = true) Integer netWorkStatus
    		){
      	Result result = null ;
      	FaceLock fl = faceLockService.getFaceLockByUUID(Long.valueOf(lockId));
        if(fl ==null) {
          	 result = new Result(ResultCodeEnum.NOT_GATEWAY.getCode(), ResultCodeEnum.NOT_GATEWAY.getDesc(),null);
           	 return new ResponseEntity<>(result, HttpStatus.OK);
        }
         fl.setNetWorkStatus(netWorkStatus);
         faceLockService.update(fl);
       	 result = new Result(ResultCodeEnum.OK.getCode(), ResultCodeEnum.OK.getDesc(),null);
       	 return new ResponseEntity<>(result, HttpStatus.OK);
    }
    public static void main(String[] args) {
    	String url = "/opt/img/20210629/1624937036334.jpg";
		System.out.println(url.replace(url.subSequence(0, url.lastIndexOf("/")), ""));
	}
}
